package com.csst.action;

import java.text.SimpleDateFormat;
import java.util.Date;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;

import com.csst.bean.Account;
import com.csst.bean.User;
import com.csst.service.AccountService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;

@Controller
public class AccountAction extends ActionSupport implements
		ModelDriven<Account>, Preparable {

	/**
	 * 
	 */
	private static final long serialVersionUID = -1241911430283073794L;

	@Resource
	private AccountService service;
	private Account account;

	public Account getModel() {
		// TODO Auto-generated method stub
		return account;
	}

	public void prepare() throws Exception {
		// TODO Auto-generated method stub
		account = new Account();
	}

	@Override
	public String execute() throws Exception {
		// TODO Auto-generated method stub
		if (null!=account.getAccpassword()) {
			ActionContext context = ActionContext.getContext();
			User user = (User) context.getSession().get("user");
			Date currentTime = new Date();
			SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
			String dateString = formatter.format(currentTime);
			account.setAtime(dateString);
			account.setMoney(0.0);
			account.setUser(user);
			service.saveAccount(account);
			return "success";
		}
		return "fail";
	}

}
